package com.guli.gulimall.gulimallproduct.service.impl;

import com.guli.gulimall.gulimallproduct.service.CategoryBrandRelationService;
import com.guli.gulimall.gulimallproduct.service.CategoryService;
import lombok.val;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.gulimallproduct.dao.BrandDao;
import com.guli.gulimall.gulimallproduct.entity.BrandEntity;
import com.guli.gulimall.gulimallproduct.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = ((String) params.get("key"));
        QueryWrapper<BrandEntity> brandEntityQueryWrapper = new QueryWrapper<BrandEntity>();
        if (!key.isEmpty()) {
            brandEntityQueryWrapper.eq("brand_id", key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                brandEntityQueryWrapper
        );

        return new PageUtils(page);
    }


    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
//    @Autowired
//    private CategoryService categoryService;

    @Override
    public boolean updateDtails(BrandEntity brand) {
        /**
         * 保证考虑冗余字段的更新
         */
        boolean check = true;
        if (!brand.getName().isEmpty()) {
            //同步更新其他关联表中的数据
            check = categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
            /**
             * todo
             * 更新其他关联表
             */
        }
        return check ? (check && this.updateById(brand)) : false;
    }

}